package com.sxbbc.plat.util;

import com.sxbbc.common.core.dto.jz.MenusVo;
import com.sxbbc.common.core.entity.jz.SysMenus;
import com.sxbbc.common.core.entity.jz.SysUser;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author leehao
 * @description 平台 session工具
 * @date 2019-04-26 10:56
 */
public class PlatSession {

    /**
     * 验证码
     */
    public static final String VALID_CODE = "session_validCode";
    /**
     * 角色id
     */
    public static final String ROLE_ID = "session_roleId";

    /**
     * 用户id
     */
    public static final String USER_ID = "session_userId";

    /**
     * 菜单
     */
    public static final String MENU_LIST = "session_menusList";

    /**
     * 用户
     */
    public static final String SYS_USER = "session_sysUser";

    /**
     * 菜单URL
     */
    public static final String MENU_URL = "session_menuUrl";

    /**
     * 菜单
     */
    public static final String MENU = "session_menu";

    /**
     * description: 退出
     *
     * @param request
     * @return void
     * @author leehao
     */
    public static void exit(HttpServletRequest request) {
        request.getSession().removeAttribute(MENU_LIST);
        request.getSession().removeAttribute(USER_ID);
        request.getSession().removeAttribute(ROLE_ID);
        request.getSession().removeAttribute(SYS_USER);
    }

    /**
     * session设置用户对象
     *
     * @param request
     * @param sysUser 用户对象
     * @return void
     */
    public static void setSysUser(HttpServletRequest request, SysUser sysUser) {
        request.getSession().removeAttribute(SYS_USER);
        request.getSession().setAttribute(SYS_USER, sysUser);
    }

    /**
     * description: 用户获取session
     *
     * @param request
     * @return com.sxbbc.common.core.entity.jz.SysUser
     * @author leehao
     */
    public static SysUser getSysUser(HttpServletRequest request) {
        SysUser sysuser = (SysUser) request.getSession().getAttribute(SYS_USER);
        return sysuser;
    }

    /**
     * description: 菜单设置session
     *
     * @param request
     * @param list    菜单list对象
     * @return void
     * @author leehao
     */
    public static void setMenusList(HttpServletRequest request, List<MenusVo> list) {
        request.getSession().removeAttribute(MENU_LIST);
        request.getSession().setAttribute(MENU_LIST, list);
    }

    /**
     * description: 菜单获取session
     *
     * @param request
     * @return java.util.List<MenusVo>
     * @author leehao
     */
    public static List<MenusVo> getMenusList(HttpServletRequest request) {
        @SuppressWarnings("unchecked")
        List<MenusVo> list = (List<MenusVo>) request.getSession().getAttribute(MENU_LIST);
        return list;
    }

    /**
     * description: 菜单URL获取session
     *
     * @param request
     * @return java.util.List<java.lang.String>
     * @author leehao
     */
    public static List<String> getMenuUrl(HttpServletRequest request) {
        @SuppressWarnings("unchecked")
        List<String> list = (List<String>) request.getSession().getAttribute(MENU_URL);
        return list;
    }

    /**
     * description: 菜单URL设置session
     *
     * @param request
     * @param list
     * @return void
     * @author leehao
     */
    public static void setMenuUrl(HttpServletRequest request, List<String> list) {
        request.getSession().removeAttribute(MENU_URL);
        request.getSession().setAttribute(MENU_URL, list);
    }

    /**
     * description: 菜单获取session
     *
     * @param request
     * @return java.util.List<SysMenus>
     * @author leehao
     */
    public static List<SysMenus> getMenu(HttpServletRequest request) {
        @SuppressWarnings("unchecked")
        List<SysMenus> list = (List<SysMenus>) request.getSession().getAttribute(MENU);
        return list;
    }

    /**
     * description: 菜单设置session
     *
     * @param request
     * @param list    菜单list对象
     * @return void
     * @author leehao
     */
    public static void setMenu(HttpServletRequest request, List<SysMenus> list) {
        request.getSession().removeAttribute(MENU);
        request.getSession().setAttribute(MENU, list);
    }

    /**
     * description: 用户id设置session
     *
     * @param request
     * @param userId  用户id
     * @return void
     * @author leehao
     */
    public static void setUserId(HttpServletRequest request, Integer userId) {
        request.getSession().removeAttribute(USER_ID);
        request.getSession().setAttribute(USER_ID, userId);
    }

    /**
     * description: 用户id获取session
     *
     * @param request
     * @return java.lang.Integer
     * @author leehao
     */
    public static Integer getUserId(HttpServletRequest request) {
        Integer userId = (Integer) request.getSession().getAttribute(USER_ID);
        return userId;
    }

    /**
     * 角色id设置session
     *
     * @param request
     * @param roleId  角色id
     * @return void
     * @author leehao
     */
    public static void setRoleId(HttpServletRequest request, Integer roleId) {
        request.getSession().removeAttribute(ROLE_ID);
        request.getSession().setAttribute(ROLE_ID, roleId);
    }

    /**
     * 角色id获取session
     *
     * @param request
     * @return java.lang.Integer
     * @author leehao
     */
    public static Integer getRoleId(HttpServletRequest request) {
        Integer roleId = (Integer) request.getSession().getAttribute(ROLE_ID);
        return roleId;
    }

    /**
     * 验证码对象设置session
     *
     * @param request
     * @param validCode 验证码
     * @return void
     * @author leehao
     */
    public static void setValidCode(HttpServletRequest request, String validCode) {
        request.getSession().removeAttribute(VALID_CODE);
        request.getSession().setAttribute(VALID_CODE, validCode);
    }

    /**
     * 验证码对象获取session
     *
     * @param request
     * @return java.lang.String
     * @author leehao
     */
    public static String getValidCode(HttpServletRequest request) {
        String validCode = (String) request.getSession().getAttribute(VALID_CODE);
        return validCode;
    }
}
